<?php

namespace app\admin\controller\site;

use app\admin\controller\AController;
use app\admin\service\RequestService;
use app\model\site\Site;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;

class SiteController extends AController
{
    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function index(RequestService $requestService): \think\response\Json
    {
        $list = $requestService->read(new Site());
        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total(),
        ]);
    }

    public function add(RequestService $requestService){
        return $requestService->add(new Site());
    }

    /**
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function edit(RequestService $requestService, Request $request){
        if ($request->isPost()){
            return $requestService->edit(new Site());
        }else{
            $id = $request->param('id');
            $info = Site::find($id);
            if (!$info) return jsonError(NOT_FOUND);
            return jsonSuccess([
                'info' => $info
            ]);
        }
    }

}
